﻿using Abp.Domain.Entities;
using Abp.UI;
using Microsoft.EntityFrameworkCore;
using shenghua.Common.CustomValueType;
using shenghua.Common.WordNodes;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.ShippingTemplates
{
    public class ShippingTemplate : WordNode, IExtendableObject
    {
        public ShippingTemplateType TemplateType { get; set; }

        public bool IsFixedFreight
        {
            get
            {
                return TemplateType == ShippingTemplateType.FixedFreight;
            }
        }

        /// <summary>
        /// 运费，首件运费
        /// </summary>
        public Money Freight { get; set; }
        /// <summary>
        /// 首件数量
        /// </summary>
        public int FirstNumber { get; set; }
        /// <summary>
        /// 续件数量
        /// </summary>
        public int AddedNumber { get; set; }
        /// <summary>
        /// 续费
        /// </summary>
        public Money AddedFreight { get; set; }

        /// <summary>
        /// 配送区域
        /// </summary>
        public ValueArray<string> Areas { get; set; }

        /// <summary>
        /// 是否允许包邮
        /// </summary>
        public bool AllowFreeShipping { get; set; }

        /// <summary>
        /// 包邮条件，之所以没有做在单独的表中，是希望可以随时拿到包邮条件
        /// </summary>
        [NotMapped]
        public List<FreeShippingCondition> FreeShippingConditions
        {
            get
            {
                return this.GetData<List<FreeShippingCondition>>("freeshippingconditions");
            }
        }

        public string ExtensionData { get; set; }

        public ShippingTemplate()
        {
            Freight = new Money();
            Areas = new ValueArray<string>();
            SetFreeShippingConditions(new List<FreeShippingCondition>());
        }

        public void SetFreeShippingConditions(List<FreeShippingCondition> freeShippingConditions)
        {
            this.SetData("freeshippingconditions", freeShippingConditions);
        }

        /// <summary>
        /// 判断是否包邮
        /// </summary>
        /// <param name="areaTag"></param>
        /// <param name="commodityTotalCount"></param>
        /// <param name="commodityTotalAmount"></param>
        /// <returns></returns>
        public bool CanFreeShipping(string areaTag, int commodityTotalCount, Money commodityTotalAmount)
        {
            foreach(var item in FreeShippingConditions)
            {
                if(string.Join(",", item.Areas).Contains(areaTag) && commodityTotalCount >= item.MinCount && commodityTotalAmount >= item.MinAmount)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取运费，不考虑包邮条件，实际使用需要结合CanFreeShipping函数
        /// </summary>
        /// <param name="areaTag"></param>
        /// <param name="commodityTotalCount"></param>
        /// <param name="commodityTotalAmount"></param>
        /// <returns></returns>
        public Money GetFreight(string areaTag, int commodityTotalCount, Money commodityTotalAmount)
        {
            if(!Areas.StringValue.Contains(areaTag))
            {
                throw new UserFriendlyException("您的地址不在指定运费模板的地区范围内");
            }

            if (TemplateType == ShippingTemplateType.FixedFreight)
            {
                return Freight;
            }
            else
            {
                if(commodityTotalCount <= FirstNumber)
                {
                    return Freight;
                }
                else
                {
                    return Freight + AddedFreight * Math.Ceiling((double)((commodityTotalCount - FirstNumber) / AddedNumber));
                }
            }
        }
    }
    
    public class FreeShippingCondition
    {
        public List<string> Areas { get; set; }
        public int MinCount { get; set; }
        public Money MinAmount { get; set; }

        public FreeShippingCondition()
        {
            Areas = new List<string>();
            MinAmount = new Money();
        }
    }

    public enum ShippingTemplateType
    {
        /// <summary>
        /// 固定运费
        /// </summary>
        FixedFreight = 0,
        /// <summary>
        /// 阶梯运费
        /// </summary>
        LadderFreight = 1
    }
}
